<!DOCTYPE html><html><head>
      <title>网格策略</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="../yangshi.css">
      
      
      
      
      
      <style>
      code[class*=language-],pre[class*=language-]{color:#333;background:0 0;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.4;-moz-tab-size:8;-o-tab-size:8;tab-size:8;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:.8em;overflow:auto;border-radius:3px;background:#f5f5f5}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal;background:#f5f5f5}.token.blockquote,.token.comment{color:#969896}.token.cdata{color:#183691}.token.doctype,.token.macro.property,.token.punctuation,.token.variable{color:#333}.token.builtin,.token.important,.token.keyword,.token.operator,.token.rule{color:#a71d5d}.token.attr-value,.token.regex,.token.string,.token.url{color:#183691}.token.atrule,.token.boolean,.token.code,.token.command,.token.constant,.token.entity,.token.number,.token.property,.token.symbol{color:#0086b3}.token.prolog,.token.selector,.token.tag{color:#63a35c}.token.attr-name,.token.class,.token.class-name,.token.function,.token.id,.token.namespace,.token.pseudo-class,.token.pseudo-element,.token.url-reference .token.variable{color:#795da3}.token.entity{cursor:help}.token.title,.token.title .token.punctuation{font-weight:700;color:#1d3e81}.token.list{color:#ed6a43}.token.inserted{background-color:#eaffea;color:#55a532}.token.deleted{background-color:#ffecec;color:#bd2c00}.token.bold{font-weight:700}.token.italic{font-style:italic}.language-json .token.property{color:#183691}.language-markup .token.tag .token.punctuation{color:#333}.language-css .token.function,code.language-css{color:#0086b3}.language-yaml .token.atrule{color:#63a35c}code.language-yaml{color:#183691}.language-ruby .token.function{color:#333}.language-markdown .token.url{color:#795da3}.language-makefile .token.symbol{color:#795da3}.language-makefile .token.variable{color:#183691}.language-makefile .token.builtin{color:#0086b3}.language-bash .token.keyword{color:#0086b3}pre[data-line]{position:relative;padding:1em 0 1em 3em}pre[data-line] .line-highlight-wrapper{position:absolute;top:0;left:0;background-color:transparent;display:block;width:100%}pre[data-line] .line-highlight{position:absolute;left:0;right:0;padding:inherit 0;margin-top:1em;background:hsla(24,20%,50%,.08);background:linear-gradient(to right,hsla(24,20%,50%,.1) 70%,hsla(24,20%,50%,0));pointer-events:none;line-height:inherit;white-space:pre}pre[data-line] .line-highlight:before,pre[data-line] .line-highlight[data-end]:after{content:attr(data-start);position:absolute;top:.4em;left:.6em;min-width:1em;padding:0 .5em;background-color:hsla(24,20%,50%,.4);color:#f4f1ef;font:bold 65%/1.5 sans-serif;text-align:center;vertical-align:.3em;border-radius:999px;text-shadow:none;box-shadow:0 1px #fff}pre[data-line] .line-highlight[data-end]:after{content:attr(data-end);top:auto;bottom:.4em}html body{font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ol,html body>ul{margin-bottom:16px}html body ol,html body ul{padding-left:2em}html body ol.no-list,html body ul.no-list{padding:0;list-style-type:none}html body ol ol,html body ol ul,html body ul ol,html body ul ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:700;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:700}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::after,html body code::before{letter-spacing:-.2em;content:'\00a0'}html body pre>code{padding:0;margin:0;word-break:normal;white-space:pre;background:0 0;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:after,html body pre code:before,html body pre tt:after,html body pre tt:before{content:normal}html body blockquote,html body dl,html body ol,html body p,html body pre,html body ul{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body code,html body pre{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview ul{list-style:disc}.markdown-preview ul ul{list-style:circle}.markdown-preview ul ul ul{list-style:square}.markdown-preview ol{list-style:decimal}.markdown-preview ol ol,.markdown-preview ul ol{list-style-type:lower-roman}.markdown-preview ol ol ol,.markdown-preview ol ul ol,.markdown-preview ul ol ol,.markdown-preview ul ul ol{list-style-type:lower-alpha}.markdown-preview .newpage,.markdown-preview .pagebreak{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center!important}.markdown-preview:not([data-for=preview]) .code-chunk .code-chunk-btn-group{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .status{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .output-div{margin-bottom:16px}.markdown-preview .md-toc{padding:0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link div,.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}.markdown-preview .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0;min-height:100vh}@media screen and (min-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{font-size:14px!important;padding:1em}}@media print{html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc{padding:0 16px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link div,html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 300px / 2);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
      <!-- The content below will be included at the end of the <head> element. --><script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function () {
    // your code here
  });
</script></head><body for="html-export">
    
    
      <div class="crossnote markdown-preview  ">
      
<p><span style="font-size: 32px; font-weight: bold;">目录</span></p>
<div class="code-chunk" data-id="code-chunk-id-0" data-cmd="toc"><div class="input-div"><div class="code-chunk-btn-group"><div class="run-btn btn btn-xs btn-primary"><span>▶︎</span></div><div class="run-all-btn btn btn-xs btn-primary">all</div></div><div class="status">running...</div></div><div class="output-div"></div></div><ul>
<li><a href="#1-%E6%8C%89%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9D%A1%E4%BB%B6%E5%85%A5%E5%9C%BA%E5%90%8E%E9%A9%AC%E4%B8%81%E5%8A%A0%E4%BB%93%E7%BD%91%E6%A0%BC">1、按自定义条件入场后马丁加仓网格</a></li>
<li><a href="#2-%E6%A0%B9%E6%8D%AE%E5%BA%95%E4%BB%93%E5%88%B7%E5%8D%95%E7%BD%91%E6%A0%BC%E5%8F%AA%E5%A4%9A">2、根据底仓刷单网格只多</a>
<ul>
<li><a href="#23-%E7%AD%96%E7%95%A5%E6%8C%82%E5%8D%95%E6%95%88%E6%9E%9C%E5%B1%95%E7%A4%BA">2.3、策略挂单效果展示</a></li>
</ul>
</li>
<li><a href="#3-%E9%AB%98%E7%BA%A7%E5%8F%8C%E5%90%91%E5%88%B7%E5%8D%95%E7%BD%91%E6%A0%BC%E9%AB%98%E9%A2%91%E5%88%B7%E6%89%8B%E7%BB%AD%E8%B4%B9">3、高级双向刷单网格（高频刷手续费）</a>
<ul>
<li><a href="#33-%E7%AD%96%E7%95%A5%E6%8C%82%E5%8D%95%E6%95%88%E6%9E%9C%E5%B1%95%E7%A4%BA">3.3、策略挂单效果展示</a></li>
<li><a href="#34-%E5%88%B7%E4%BA%8630%E5%88%86%E9%92%9F%E6%95%88%E6%9E%9C%E8%BF%98%E8%A1%8C">3.4、刷了30分钟效果还行</a></li>
<li><a href="#35-%E9%AB%98%E7%BA%A7%E5%88%B7%E5%8D%95%E7%BD%91%E6%A0%BC%E5%B7%A5%E4%BD%9C%E5%8C%BA">3.5、高级刷单网格工作区</a></li>
</ul>
</li>
</ul>
<hr>
<p><a href="https://www.bilibili.com/video/BV1TN411n7L4/" target="_blank" style="font-size:16px;">策略使用视频教程</a></p>
<hr>
<p><strong>注意：使用这种高频策略需关闭防信号闪烁功能，因为这种高频会在几个tick内平凡下单，不关闭就会就会异常报错：</strong><br>
<img src="../img/%E7%BD%91%E6%A0%BC%E7%AD%96%E7%95%A5/%E9%98%B2%E4%BF%A1%E5%8F%B7%E9%97%AA%E7%83%81%E5%85%B3%E9%97%AD.png" alt="在这里插入图片描述"></p>
<hr>
<h3 id="1-按自定义条件入场后马丁加仓网格">1、按自定义条件入场后马丁加仓网格 </h3>
<p>1.1、策略逻辑见策略前面注释，对应测试工作区为“clcsh-按条件加仓网格.xlsx”</p>
<p>1.2、策略代码</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token keyword keyword-from">from</span> tool<span class="token punctuation">.</span>gongju <span class="token keyword keyword-import">import</span> <span class="token operator">*</span>
<span class="token keyword keyword-from">from</span> tool<span class="token punctuation">.</span>zhibiao <span class="token keyword keyword-import">import</span> ma_z

<span class="token keyword keyword-def">def</span> 条件加仓网格策略_v1<span class="token punctuation">(</span>cl_sj<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    启动方式，为tick驱动
     一：开仓逻辑可自行设置胜率高的入场点进入，此策略下单点胜率越高越赚钱：

    # 初次开仓(当前策略出场下单逻辑
    if volume[-1] &gt; volume[-2] and chicang == 0 and cl_sj.sjb['cczt'] == 0:  # 成交量大于5日的3倍，且为空仓
        if yunxukaicang and close[-1] &lt; close[-2]:  # 满足初次开多
            pass

    else:  # 开空
    if yunxukaicang and close[-1] &gt; close[-2]:  # 满足初次开空条件
        pass

   3.止盈：盈利大于等于8跳止盈

    二：加仓逻辑
   1.距离上次开仓价格亏损10跳、加仓2手 ，持仓均价盈利大于等于6跳止盈
   2.距离上次开仓价格亏损10跳、加仓4手 ，持仓均价盈利大于等于6跳止盈
   3.距离上次开仓价格亏损10跳、加仓8手 ，持仓均价盈利大于等于6跳止盈
   4.距离上次开仓价格亏损10跳、加仓16手，持仓均价盈利大于等于6跳止盈

   # 加仓数量可调整，在下面的程序段调整加仓列表
   jcsllb = [2, 4, 8, 16, 1]  # 每次加仓数量列表

    三：止损逻辑
    1.距离上次开仓点位亏损10跳、对手价止损平掉所有持仓。
    2.收盘前一分钟对手价平掉所有持仓并且不开新仓.
    注意事项：1.每次开新仓的同时就挂好加仓单和止盈单
            2.源码每行代码后汉字备注内容，（便于自己随时微调，不用总去麻烦你）
            3.实时价模型，只要满足条件一根K线可以发出多次信号 ，出信号一秒下单不复核

    参数可调设置： 第一次开仓止盈参数 1-50   8
                加仓开仓后止盈参数 1-50   6
                加仓距离上次开仓间隔点数 1-50  10

    四：晚上收盘和白天收盘都清仓

    """</span>
    <span class="token comment"># 数据初始化，</span>
    datetime <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>values
    close <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>close<span class="token punctuation">.</span>values
    Open <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">.</span>values
    volume <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>volume<span class="token punctuation">.</span>values
    chicang <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangzhuangtai'</span><span class="token punctuation">]</span>
    chicangshuliang <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangshuliang'</span><span class="token punctuation">]</span> <span class="token operator">*</span> chicang
    shijian <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>time_to_datetime<span class="token punctuation">(</span>datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%H%M%S"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 当前时间</span>
    zhbh <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 开仓账户编号</span>

    <span class="token comment"># 参数设置区</span>
    ss <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 初次开仓数量</span>
    dyckczy <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 第一次开仓止盈参数 1-50   8</span>
    jchzy <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓开仓后止盈参数 1-50   6</span>
    jcjj <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓距离上次开仓间隔点数 1-50  10</span>
    zsj <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span>  <span class="token comment"># 最后一次加仓后止损价</span>
    wtpy <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span>  <span class="token comment"># 委托偏移</span>
    ypqcsj <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span>  <span class="token comment"># 夜盘清仓时间</span>
    volmazq <span class="token operator">=</span> <span class="token number">2</span>  <span class="token comment"># 成交量平均周期</span>
    cjlbl <span class="token operator">=</span> <span class="token number">1</span>  <span class="token comment"># 成交量倍率</span>
    cckcbz <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 初次开仓标志，为1为有条件开仓</span>
    jcsllb <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 每次加仓数量列表</span>
    qingcangshijian <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">145800</span><span class="token punctuation">,</span> ypqcsj<span class="token punctuation">]</span>  <span class="token comment"># 清仓时间</span>
    jyhe <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

    <span class="token keyword keyword-if">if</span> <span class="token string">'cjl'</span> <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">:</span>  <span class="token comment"># 初始化时执行一次，用于初始化缓存字典</span>
        chengjiaoliang <span class="token operator">=</span> ma_z<span class="token punctuation">(</span>volume<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">60</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> volmazq<span class="token punctuation">)</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cjl'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span>chengjiaoliang<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 初始化目标仓位</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 加仓数量索引</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>get_zuixiaobiandong<span class="token punctuation">(</span>jyhe<span class="token punctuation">)</span>   <span class="token comment"># 最小变动单位</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcgdjg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 存放加仓挂单价</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 存放加仓后止盈挂单价</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 持仓状态</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cckcsj'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">8627352304</span>  <span class="token comment"># 初次开仓时间</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8888'</span>  <span class="token comment"># 止盈挂单id</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_jc'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8888'</span>  <span class="token comment"># 止盈挂单id</span>
        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8888'</span>  <span class="token comment"># 初次挂单id</span>
    dyckczy <span class="token operator">=</span> dyckczy <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 第一次开仓止盈参数 1-50   8</span>
    jchzy <span class="token operator">=</span> jchzy <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓开仓后止盈参数 1-50   6</span>
    jcjj <span class="token operator">=</span> jcjj <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓距离上次开仓间隔点数 1-50  10</span>
    zsj <span class="token operator">=</span> zsj <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 最后一次加仓后止损价</span>
    ccwtpy <span class="token operator">=</span> <span class="token number">3</span><span class="token operator">*</span><span class="token number">3</span><span class="token operator">*</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 出场委托偏移</span>

    <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>jiaoyiqidong<span class="token punctuation">:</span>  <span class="token comment"># 启动交易才执行下面程序</span>
        <span class="token comment"># 允许交易时间控制</span>
        yunxukaicang <span class="token operator">=</span> <span class="token punctuation">(</span>shijian <span class="token operator">&gt;</span> <span class="token number">90200</span> <span class="token keyword keyword-and">and</span> shijian <span class="token operator">&lt;</span> <span class="token number">144800</span><span class="token punctuation">)</span> <span class="token keyword keyword-or">or</span> <span class="token punctuation">(</span>shijian <span class="token operator">&gt;</span> <span class="token number">210200</span> <span class="token keyword keyword-and">and</span> shijian <span class="token operator">&lt;</span> <span class="token number">235950</span><span class="token punctuation">)</span> <span class="token keyword keyword-or">or</span> \
                       <span class="token punctuation">(</span>shijian <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> shijian <span class="token operator">&lt;</span> <span class="token number">23000</span><span class="token punctuation">)</span>   <span class="token comment"># 允许开仓时间</span>
        <span class="token comment"># 初次开仓</span>
        <span class="token keyword keyword-if">if</span> volume<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&gt;</span> volume<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token keyword keyword-and">and</span> chicang <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 成交量大于5日的3倍，且为空仓</span>
            <span class="token keyword keyword-if">if</span> yunxukaicang <span class="token keyword keyword-and">and</span> close<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> close<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>  <span class="token comment"># 满足初次开多条件</span>

                kaicangjiage <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'BidsJ'</span><span class="token punctuation">]</span> <span class="token operator">+</span> wtpy<span class="token operator">*</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 取排队价+偏移</span>
                cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> kaicangjiage
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'初次开多='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> kaicangjiage<span class="token punctuation">,</span> ss<span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                dycgdjzy <span class="token operator">=</span> kaicangjiage <span class="token operator">+</span> dyckczy  <span class="token comment"># 第一次止盈挂单价</span>
                gdjc1 <span class="token operator">=</span> kaicangjiage <span class="token operator">-</span> jcjj  <span class="token comment"># 第一次挂单加仓价</span>
                gdjc2 <span class="token operator">=</span> gdjc1 <span class="token operator">-</span> jcjj  <span class="token comment"># 第二次挂单加仓价</span>
                gdjc3 <span class="token operator">=</span> gdjc2 <span class="token operator">-</span> jcjj  <span class="token comment"># 第三次挂单加仓价</span>
                gdjc4 <span class="token operator">=</span> gdjc3 <span class="token operator">-</span> jcjj  <span class="token comment"># 第三次挂单加仓价</span>
                zhisun <span class="token operator">=</span> gdjc4 <span class="token operator">-</span> zsj  <span class="token comment"># 最后一次加仓后的止损价</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>gdjc1<span class="token punctuation">,</span> gdjc2<span class="token punctuation">,</span> gdjc3<span class="token punctuation">,</span> gdjc4<span class="token punctuation">,</span> zhisun<span class="token punctuation">]</span>  <span class="token comment"># 计算接下来加仓挂单价格</span>
                jczyj1 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss<span class="token operator">+</span>gdjc1<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> jchzy
                jczyj2 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss<span class="token operator">+</span>gdjc1<span class="token operator">*</span><span class="token number">2</span><span class="token operator">+</span>gdjc2<span class="token operator">*</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">7</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> jchzy
                jczyj3 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss <span class="token operator">+</span> gdjc1<span class="token operator">*</span><span class="token number">2</span> <span class="token operator">+</span> gdjc2<span class="token operator">*</span><span class="token number">4</span><span class="token operator">+</span>gdjc3<span class="token operator">*</span><span class="token number">8</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">15</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> jchzy
                jczyj4 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss <span class="token operator">+</span> gdjc1<span class="token operator">*</span><span class="token number">2</span> <span class="token operator">+</span> gdjc2<span class="token operator">*</span><span class="token number">4</span> <span class="token operator">+</span> gdjc3<span class="token operator">*</span><span class="token number">8</span> <span class="token operator">+</span> gdjc4<span class="token operator">*</span><span class="token number">16</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">31</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> jchzy
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>dycgdjzy<span class="token punctuation">,</span> jczyj1<span class="token punctuation">,</span> jczyj2<span class="token punctuation">,</span> jczyj3<span class="token punctuation">,</span> jczyj4<span class="token punctuation">]</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cckcsj'</span><span class="token punctuation">]</span> <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
                cckcbz <span class="token operator">=</span> <span class="token number">1</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>  <span class="token comment"># 开空</span>
                <span class="token keyword keyword-if">if</span> yunxukaicang <span class="token keyword keyword-and">and</span> close<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&gt;</span> close<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>  <span class="token comment"># 满足初次开空条件</span>

                    kaicangjiage <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'AsksJ'</span><span class="token punctuation">]</span> <span class="token operator">-</span> wtpy <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span>  <span class="token comment"># 取排队价+偏移</span>
                    cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> kaicangjiage
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span>  <span class="token comment"># 开空</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'初次开空='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> kaicangjiage<span class="token punctuation">,</span> ss<span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span>
                    dycgdjzy <span class="token operator">=</span> kaicangjiage <span class="token operator">-</span> dyckczy  <span class="token comment"># 第一次止盈挂单价</span>
                    gdjc1 <span class="token operator">=</span> kaicangjiage <span class="token operator">+</span> jcjj  <span class="token comment"># 第一次挂单加仓价</span>
                    gdjc2 <span class="token operator">=</span> gdjc1 <span class="token operator">+</span> jcjj  <span class="token comment"># 第二次挂单加仓价</span>
                    gdjc3 <span class="token operator">=</span> gdjc2 <span class="token operator">+</span> jcjj  <span class="token comment"># 第三次挂单加仓价</span>
                    gdjc4 <span class="token operator">=</span> gdjc3 <span class="token operator">+</span> jcjj  <span class="token comment"># 第三次挂单加仓价</span>
                    zhisun <span class="token operator">=</span> gdjc4 <span class="token operator">+</span> zsj  <span class="token comment"># 最后一次加仓后的止损价</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>gdjc1<span class="token punctuation">,</span> gdjc2<span class="token punctuation">,</span> gdjc3<span class="token punctuation">,</span> gdjc4<span class="token punctuation">,</span> zhisun<span class="token punctuation">]</span>  <span class="token comment"># 计算接下来加仓挂单价格</span>
                    jczyj1 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss <span class="token operator">+</span> gdjc1<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">-</span> jchzy
                    jczyj2 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss <span class="token operator">+</span> gdjc1<span class="token operator">*</span><span class="token number">2</span> <span class="token operator">+</span> gdjc2<span class="token operator">*</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">7</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">-</span> jchzy
                    jczyj3 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss <span class="token operator">+</span> gdjc1<span class="token operator">*</span><span class="token number">2</span> <span class="token operator">+</span> gdjc2<span class="token operator">*</span><span class="token number">4</span> <span class="token operator">+</span> gdjc3<span class="token operator">*</span><span class="token number">8</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">15</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">-</span> jchzy
                    jczyj4 <span class="token operator">=</span> <span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>kaicangjiage<span class="token operator">*</span>ss <span class="token operator">+</span> gdjc1<span class="token operator">*</span><span class="token number">2</span> <span class="token operator">+</span> gdjc2<span class="token operator">*</span><span class="token number">4</span> <span class="token operator">+</span> gdjc3<span class="token operator">*</span><span class="token number">8</span> <span class="token operator">+</span> gdjc4<span class="token operator">*</span><span class="token number">16</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">31</span><span class="token punctuation">)</span> <span class="token operator">/</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'hydx'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">-</span> jchzy
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>dycgdjzy<span class="token punctuation">,</span> jczyj1<span class="token punctuation">,</span> jczyj2<span class="token punctuation">,</span> jczyj3<span class="token punctuation">,</span> jczyj4<span class="token punctuation">]</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cckcsj'</span><span class="token punctuation">]</span> <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
                    cckcbz <span class="token operator">=</span> <span class="token number">1</span>

    <span class="token comment"># 账户仓位数据获取</span>
    ccss <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>get_chicang<span class="token punctuation">(</span>jyhe<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    <span class="token comment"># 账户持仓放入显示</span>
    <span class="token keyword keyword-if">if</span> ccss <span class="token operator">!=</span> chicangshuliang<span class="token punctuation">:</span>
        MubiaoCangwei4_bxd<span class="token punctuation">(</span>ccss<span class="token punctuation">,</span> Open<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">)</span>

    <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>jiaoyiqidong<span class="token punctuation">:</span>  <span class="token comment"># 允许交易</span>
        <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token keyword keyword-and">and</span> ccss <span class="token operator">==</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">&lt;=</span> <span class="token number">4</span><span class="token punctuation">:</span>  <span class="token comment"># 挂多单</span>
                <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行止盈挂单</span>
                    <span class="token comment"># if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zy']]['kcdsl'] != 0:  # 可撤单数量不为0</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤上次止盈单</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>

                cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>  <span class="token comment"># 多止盈挂单价格</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>  <span class="token comment"># 多止盈平仓挂单</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'止盈挂单多='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>

                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span><span class="token operator">+</span>jcsllb<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>  <span class="token comment"># 下一次的目标仓位</span>

                <span class="token comment"># 初次仓开仓成交后进行挂单操作</span>
                <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> <span class="token number">4</span><span class="token punctuation">:</span>  <span class="token comment"># 挂多单加仓</span>
                    cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_jc'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> jcsllb<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 加多</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'加仓挂单多='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> jcsllb<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span>  <span class="token comment"># 加仓数量索引加1</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                ccss2 <span class="token operator">=</span> <span class="token builtin">abs</span><span class="token punctuation">(</span>ccss<span class="token punctuation">)</span>
                <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token keyword keyword-and">and</span> ccss2 <span class="token operator">==</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">&lt;=</span> <span class="token number">4</span><span class="token punctuation">:</span>  <span class="token comment"># 挂空单</span>
                    <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行止盈挂单</span>
                        <span class="token comment"># if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zy']]['kcdsl'] != 0:  # 可撤单数量不为0</span>
                        cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤上次止盈单</span>
                        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>

                    cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>  <span class="token comment"># 多止盈挂单价格</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ccss2<span class="token punctuation">)</span>  <span class="token comment"># 空止盈平仓挂单</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'止盈挂单空='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zygdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ccss2<span class="token punctuation">)</span>

                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">+</span> jcsllb<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>  <span class="token comment"># 下一次的目标仓位</span>

                    <span class="token comment"># 初次仓开仓成交后进行挂单操作</span>
                    <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">&lt;</span> <span class="token number">4</span><span class="token punctuation">:</span>  <span class="token comment"># 挂多单加仓</span>
                        cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
                        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_jc'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> jcsllb<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 加空</span>
                        <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'加仓挂单空='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                              jcsllb<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span>  <span class="token comment"># 加仓数量索引加1</span>

        <span class="token comment"># 止损</span>
        <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">&gt;</span> <span class="token number">4</span><span class="token punctuation">:</span>  <span class="token comment"># 已经加仓4次，启动止损</span>
            <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 多止损</span>
                <span class="token comment"># print('多准备止损止损价=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['gdjg'][-1], ccss)</span>
                <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">&lt;=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span>cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span><span class="token operator">-</span>ccwtpy  <span class="token comment"># 止损价格</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 开仓状态清0</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 仓位索引清零</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 目标仓位调整为初始状态</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'多单止损='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span><span class="token operator">-</span>ccwtpy<span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 空止损</span>
                    <span class="token comment"># print('空准备止损止损价=', cl_sj.jiaoyishezhi[3][0], cl_sj.sjb['gdjg'][-1], ccss)</span>
                    <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">&gt;=</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'gdjg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                        cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
                        cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">+</span> ccwtpy  <span class="token comment"># 止损价格</span>
                        cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token builtin">abs</span><span class="token punctuation">(</span>ccss<span class="token punctuation">)</span><span class="token punctuation">)</span>
                        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 开仓状态清0</span>
                        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 仓位索引清零</span>
                        cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 目标仓位调整为初始状态</span>
                        <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'空单止损='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">+</span> ccwtpy<span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>

        <span class="token comment"># 收盘清仓</span>
        <span class="token keyword keyword-if">if</span> shijian <span class="token keyword keyword-in">in</span> qingcangshijian<span class="token punctuation">:</span>  <span class="token comment"># 等于清仓时间</span>
            <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 多止损</span>
                cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_jc'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
                cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">-</span> ccwtpy  <span class="token comment"># 止损价格</span>
                cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 开仓状态清0</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 仓位索引清零</span>
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 目标仓位调整为初始状态</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'收盘清仓多='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">-</span> ccwtpy<span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 空止损</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_jc'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">+</span> ccwtpy  <span class="token comment"># 止损价格</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token builtin">abs</span><span class="token punctuation">(</span>ccss<span class="token punctuation">)</span><span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 开仓状态清0</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 仓位索引清零</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 目标仓位调整为初始状态</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">'收盘清仓空='</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span> <span class="token operator">+</span> ccwtpy<span class="token punctuation">,</span> ccss<span class="token punctuation">)</span>

        <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 已进行止盈挂单</span>
            <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行止盈挂单</span>
                <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_zy'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 可撤单数量为0，代表止盈挂单已成交</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_jc'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤到加仓挂单</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 开仓状态清0</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 仓位索引清零</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 目标仓位调整为初始状态</span>

        <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token string">'8888'</span> <span class="token keyword keyword-and">and</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> \
                cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 初次下单60秒不成交撤单</span>
            <span class="token keyword keyword-if">if</span> cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cckcsj'</span><span class="token punctuation">]</span> <span class="token operator">&gt;</span> <span class="token number">60</span><span class="token punctuation">:</span>  <span class="token comment"># 已进行止盈挂单</span>
                    cl_sj<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>zhbh<span class="token punctuation">]</span><span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'xdid_ccxd'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤初次挂单</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.2</span><span class="token punctuation">)</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 开仓状态清0</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 仓位索引清零</span>
                    cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span> <span class="token operator">=</span> ss  <span class="token comment"># 目标仓位调整为初始状态</span>

    <span class="token keyword keyword-return">return</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'jcslsy'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'mbcw'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'cczt'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl_sj<span class="token punctuation">.</span>tick<span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span>


</code></pre><hr>
<h3 id="2-根据底仓刷单网格只多">2、根据底仓刷单网格只多 </h3>
<p>2.1、策略逻辑见策略前面注释，对应测试工作区为“clcsh-根据底仓单挂网格策略.xlsx”</p>
<p>2.2、策略代码</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token keyword keyword-from">from</span> tool<span class="token punctuation">.</span>gongju <span class="token keyword keyword-import">import</span> <span class="token operator">*</span>
<span class="token keyword keyword-from">from</span> tool<span class="token punctuation">.</span>zhibiao <span class="token keyword keyword-import">import</span> ma_z

<span class="token keyword keyword-def">def</span> 根据底仓单挂网格交易多_1t<span class="token punctuation">(</span>cl<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    启动方式，为轮询
    需要开空策略需自己改造，这种网格是应对基本开底仓后，日常刷单用的。
    主观判断建好仓位后建好底仓后，价格反方向运动n1跳加仓1手，盈利n跳平一手，反复操作，加仓和止盈都为挂单,只多,单挂，实盘持仓不会越来越大
    1、过夜跳空开空多少就补多少
    2、底仓被止盈完后，就按底仓方向开一手，一直这样循环
    3、每次都加仓1手

    """</span>
    <span class="token comment"># 数据提取区</span>
    Open <span class="token operator">=</span> cl<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">.</span>values  <span class="token comment"># 开盘价np列表</span>
    Datetime <span class="token operator">=</span> cl<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>values  <span class="token comment"># 日期时间np列表，为时间戳</span>
    riqishijian <span class="token operator">=</span> time_to_datetime<span class="token punctuation">(</span>Datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    shijian <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>riqishijian<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%H%M%S"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 当前时间,格式为20210828</span>
    chicang <span class="token operator">=</span> cl<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangzhuangtai'</span><span class="token punctuation">]</span>
    chicangshuliang <span class="token operator">=</span> cl<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangshuliang'</span><span class="token punctuation">]</span> <span class="token operator">*</span> chicang
    <span class="token comment"># kaicangjia = cl.openingdata['kaicangjia']  # 开仓价格</span>

    <span class="token comment"># 参数设置区</span>
    ss <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 开仓数量</span>
    n1 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓止盈价格间隔，为跳</span>
    夜盘收盘时间 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
    清零数据库 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span>  <span class="token comment"># 为0无用，为1时初始化数据库</span>
    <span class="token comment"># 多空控制 = cl.jiaoyishezhi[5][2]  # 0为多，1为空</span>
    zhanghu <span class="token operator">=</span> cl<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span>

    <span class="token comment"># 指标计算区</span>
    <span class="token comment"># 初始化，初始化策略，和更新时运行一次</span>
    <span class="token keyword keyword-if">if</span> <span class="token string">'zxbd'</span> <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">:</span>  <span class="token comment"># 初始化时执行一次，用于初始化缓存字典</span>
        <span class="token keyword keyword-if">if</span> <span class="token keyword keyword-not">not</span> shujuku<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token keyword keyword-or">or</span> 清零数据库 <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 数据库中没有此字段，初始化数据库</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>get_zuixiaobiandong<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 交易合约最小变动单位</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8'</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 0，为初始状态，1为收盘，2为开盘</span>
            shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>

        durushuju <span class="token operator">=</span> shujuku<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
        cl<span class="token punctuation">.</span>sjb <span class="token operator">=</span> ast<span class="token punctuation">.</span>literal_eval<span class="token punctuation">(</span>durushuju<span class="token punctuation">)</span>

    n1 <span class="token operator">=</span> n1 <span class="token operator">*</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span>
    ccss <span class="token operator">=</span> <span class="token punctuation">(</span>chazhaoheyue_duiyingchicang<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>df_cc<span class="token punctuation">,</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    <span class="token comment"># 账户持仓放入显示</span>
    <span class="token keyword keyword-if">if</span> ccss <span class="token operator">!=</span> chicangshuliang<span class="token punctuation">:</span>
        MubiaoCangwei4_bxd<span class="token punctuation">(</span>ccss<span class="token punctuation">,</span> Open<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl<span class="token punctuation">)</span>

    <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>shangcishijian <span class="token operator">!=</span> Datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span>  <span class="token comment"># 收盘运行</span>
        shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 持久化全局变量</span>

    <span class="token comment"># zuixinjia = cl.tick['LastPrice']  # 最新价</span>
    zuixinjia <span class="token operator">=</span> <span class="token punctuation">(</span>DuquRedis_HQ<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span>
    <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>jiaoyiqidong<span class="token punctuation">:</span>  <span class="token comment"># 更新k线时执行，一般用与计算指标</span>
        <span class="token comment"># 交易逻辑执行区</span>
        <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token keyword keyword-and">and</span> <span class="token punctuation">(</span>shijian <span class="token operator">==</span> <span class="token number">210000</span> <span class="token keyword keyword-or">or</span> shijian <span class="token operator">==</span> <span class="token number">90000</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            tk <span class="token operator">=</span> zuixinjia <span class="token operator">-</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span>  <span class="token comment">#</span>
            lsblzt <span class="token operator">=</span> <span class="token boolean">False</span>
            <span class="token keyword keyword-if">if</span> tk <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                lsbl <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>ccss <span class="token operator">-</span> tk <span class="token operator">/</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 跳空高开应该开多少手</span>
                <span class="token keyword keyword-if">if</span> lsbl <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia <span class="token operator">-</span> n1  <span class="token comment"># 下单价格</span>
                    zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> lsbl<span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                    lsblzt <span class="token operator">=</span> <span class="token boolean">True</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 跳空低开补平多"</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                lsbl <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">abs</span><span class="token punctuation">(</span>tk <span class="token operator">/</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-if">if</span> lsbl <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia<span class="token operator">+</span>n1  <span class="token comment"># 下单价格</span>
                    zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> lsbl<span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                    lsblzt <span class="token operator">=</span> <span class="token boolean">True</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 跳空低开补开多"</span><span class="token punctuation">)</span>

            <span class="token keyword keyword-if">if</span> lsblzt<span class="token punctuation">:</span>
                zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
                ccss <span class="token operator">=</span> <span class="token punctuation">(</span>chazhaoheyue_duiyingchicang<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>df_cc<span class="token punctuation">,</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">2</span>

        <span class="token comment"># 初次挂单,或补单挂单</span>
        <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword keyword-or">or</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span>  <span class="token comment"># or cl.sjb['挂单状态'] == 1:</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                lsbl <span class="token operator">=</span> <span class="token number">10</span> <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;=</span> <span class="token number">10</span> <span class="token keyword keyword-else">else</span> ccss
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>lsbl<span class="token punctuation">)</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">+</span> n1
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token operator">-</span>n1
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 初始化开后盘状态</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"初始挂单"</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 底仓没有了进行补仓</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia<span class="token operator">+</span>n1
                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>
                zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂"</span><span class="token punctuation">)</span>

        <span class="token comment"># 有成交后挂单</span>
        <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行开仓挂单</span>
                <span class="token keyword keyword-if">if</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 开仓已成交，补挂平仓</span>
                    <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">10</span><span class="token punctuation">:</span>
                        zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤上次止盈单</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

                    <span class="token keyword keyword-if">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kpjg'</span><span class="token punctuation">]</span> <span class="token operator">+</span> n1
                        cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span>  ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                        <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">:</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> n1
                            cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂开仓"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                        <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 开仓挂单已成交"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行平仓挂单</span>
                <span class="token keyword keyword-if">if</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 平仓挂单已成交</span>
                    <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">10</span><span class="token punctuation">:</span>
                        zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 开仓挂单太多进行测单</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

                    <span class="token keyword keyword-if">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>  <span class="token comment"># 底仓为1手，已成交的话，这里的ccss等于1，但实际已经为空仓了</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kpjg'</span><span class="token punctuation">]</span> <span class="token operator">-</span> n1
                        cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                        <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span>  ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                        <span class="token keyword keyword-if">if</span> <span class="token number">0</span> <span class="token operator">&lt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">10</span> <span class="token keyword keyword-and">and</span> ccss <span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">+</span> n1
                            cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂平仓"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                        <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 平仓已成交"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">:</span>
            zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-if">if</span> shijian <span class="token operator">==</span> <span class="token number">145950</span> <span class="token keyword keyword-or">or</span> shijian <span class="token operator">==</span> 夜盘收盘时间<span class="token punctuation">:</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 收盘工作,记录收盘价撤单等"</span><span class="token punctuation">)</span>

            shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span> <span class="token operator">+</span> cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token keyword keyword-return">return</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> zuixinjia<span class="token punctuation">,</span>  cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>


</code></pre><h4 id="23-策略挂单效果展示">2.3、策略挂单效果展示 </h4>
<p><img src="../img/%E7%BD%91%E6%A0%BC%E7%AD%96%E7%95%A5/%E5%8D%95%E6%8C%82%E7%BD%91%E6%A0%BC%E6%8C%82%E5%8D%952.png" alt="在这里插入图片描述"></p>
<hr>
<h3 id="3-高级双向刷单网格高频刷手续费">3、高级双向刷单网格（高频刷手续费） </h3>
<p>2.1、策略逻辑见策略前面注释，<strong>一般需多空两个策略共同使用（也可以只用一个策略做一个方向），找两个相关性很高的合约，一个做多一个做空，如可以找一个主力合约对应的次主力合约。</strong> 对应测试工作区为“clcsh-根据底仓单挂网格策略.xlsx”</p>
<p>2.2、策略代码</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># -*- coding: utf-8 -*-</span>
<span class="token keyword keyword-from">from</span> tool<span class="token punctuation">.</span>gongju <span class="token keyword keyword-import">import</span> <span class="token operator">*</span>
<span class="token keyword keyword-from">from</span> tool<span class="token punctuation">.</span>zhibiao <span class="token keyword keyword-import">import</span> ma_z


<span class="token keyword keyword-def">def</span> 根据底仓单挂网格多_rn<span class="token punctuation">(</span>cl<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    启动方式，为轮询
    主观判断建好仓位后建好底仓后，价格反方向运动n1跳加仓1手，盈利n跳平一手，反复操作，加仓和止盈都为挂单,只多,单挂，实盘持仓不会越来越大
    1、过夜跳空开空多少就补多少
    2、底仓被止盈完后，就按底仓方向开一手，一直这样循环
    3、每次都加仓1手
    收盘清仓
    """</span>
    <span class="token comment"># 数据提取区</span>
    Open <span class="token operator">=</span> cl<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">.</span>values  <span class="token comment"># 开盘价np列表</span>
    Datetime <span class="token operator">=</span> cl<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>values  <span class="token comment"># 日期时间np列表，为时间戳</span>
    riqishijian <span class="token operator">=</span> time_to_datetime<span class="token punctuation">(</span>Datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    shijian <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>riqishijian<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%H%M%S"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 当前时间,格式为20210828</span>
    chicang <span class="token operator">=</span> cl<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangzhuangtai'</span><span class="token punctuation">]</span>
    chicangshuliang <span class="token operator">=</span> cl<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangshuliang'</span><span class="token punctuation">]</span> <span class="token operator">*</span> chicang

    <span class="token comment"># 参数设置区</span>
    ss <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 开仓数量</span>
    n1 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓止盈价格间隔，为跳</span>
    夜盘收盘时间 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
    清零数据库 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span>  <span class="token comment"># 为0无用，为1时初始化数据库</span>
    开仓挂单数 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span>

    zhanghu <span class="token operator">=</span> cl<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
    heyue <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 交易的合约</span>
    不交易时间 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">210000</span><span class="token punctuation">,</span> <span class="token number">210100</span><span class="token punctuation">,</span> <span class="token number">210200</span><span class="token punctuation">,</span> <span class="token number">210300</span><span class="token punctuation">,</span> <span class="token number">210400</span><span class="token punctuation">,</span> <span class="token number">90000</span><span class="token punctuation">,</span> <span class="token number">90100</span><span class="token punctuation">,</span> <span class="token number">90200</span><span class="token punctuation">,</span> <span class="token number">90300.90400</span><span class="token punctuation">,</span> <span class="token number">145700</span><span class="token punctuation">,</span> <span class="token number">145800</span><span class="token punctuation">,</span> <span class="token number">145900</span><span class="token punctuation">,</span> <span class="token number">225700</span><span class="token punctuation">,</span>
             <span class="token number">225800</span><span class="token punctuation">,</span> <span class="token number">225900</span><span class="token punctuation">]</span>
    <span class="token comment"># 指标计算区</span>
    <span class="token comment"># 初始化，初始化策略，和更新时运行一次</span>
    <span class="token keyword keyword-if">if</span> <span class="token string">'zxbd'</span> <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">:</span>  <span class="token comment"># 初始化时执行一次，用于初始化缓存字典</span>
        <span class="token keyword keyword-if">if</span> <span class="token keyword keyword-not">not</span> shujuku<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token keyword keyword-or">or</span> 清零数据库 <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 数据库中没有此字段，初始化数据库</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>get_zuixiaobiandong<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 交易合约最小变动单位</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8'</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 0，为初始状态，1为收盘，2为开盘</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span>
            shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>

        durushuju <span class="token operator">=</span> shujuku<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
        cl<span class="token punctuation">.</span>sjb <span class="token operator">=</span> ast<span class="token punctuation">.</span>literal_eval<span class="token punctuation">(</span>durushuju<span class="token punctuation">)</span>

    n1 <span class="token operator">=</span> n1 <span class="token operator">*</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span>
    ccss <span class="token operator">=</span> <span class="token punctuation">(</span>chazhaoheyue_duiyingchicang<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>df_cc<span class="token punctuation">,</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    <span class="token comment"># 账户持仓放入显示</span>
    <span class="token keyword keyword-if">if</span> ccss <span class="token operator">!=</span> chicangshuliang<span class="token punctuation">:</span>
        MubiaoCangwei4_bxd<span class="token punctuation">(</span>ccss<span class="token punctuation">,</span> Open<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl<span class="token punctuation">)</span>

    <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>shangcishijian <span class="token operator">!=</span> Datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span>  <span class="token comment"># 收盘运行</span>
        shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 持久化全局变量</span>

    <span class="token comment"># zuixinjia = cl.tick['LastPrice']  # 最新价</span>
    zuixinjia <span class="token operator">=</span> <span class="token punctuation">(</span>DuquRedis_HQ<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span>
    <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>jiaoyiqidong<span class="token punctuation">:</span>  <span class="token comment"># 更新k线时执行，一般用与计算指标</span>
        <span class="token comment"># 初次挂单,或补单挂单</span>
        <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword keyword-or">or</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                lsbl <span class="token operator">=</span> 开仓挂单数 <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;=</span> 开仓挂单数 <span class="token keyword keyword-else">else</span> ccss
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>lsbl<span class="token punctuation">)</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">+</span> n1
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>开仓挂单数<span class="token punctuation">)</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token operator">-</span>n1
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 初始化开后盘状态</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> <span class="token string">"初始挂单"</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>  <span class="token comment"># 底仓没有了进行补仓</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia<span class="token operator">+</span>n1
                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>
                zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂"</span><span class="token punctuation">)</span>

        <span class="token comment"># 有成交后挂单</span>
        <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行开仓挂单</span>
                    <span class="token keyword keyword-if">if</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 开仓已成交，补挂平仓</span>
                        <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> 开仓挂单数<span class="token punctuation">:</span>
                            zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤上次止盈单</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

                        <span class="token keyword keyword-if">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kpjg'</span><span class="token punctuation">]</span> <span class="token operator">+</span> n1
                            cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span>  ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                            <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> 开仓挂单数<span class="token punctuation">:</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> n1
                                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂开仓"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 开仓挂单已成交"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>  <span class="token comment"># 开仓挂单列表为空</span>
                <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">True</span>
                    <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                        zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>

            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行平仓挂单</span>
                    <span class="token keyword keyword-if">if</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 平仓挂单已成交</span>
                        <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> 开仓挂单数<span class="token punctuation">:</span>
                            zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 开仓挂单太多进行测单</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

                        <span class="token keyword keyword-if">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>  <span class="token comment"># 底仓为1手，已成交的话，这里的ccss等于1，但实际已经为空仓了</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kpjg'</span><span class="token punctuation">]</span> <span class="token operator">-</span> n1
                            cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span>  ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开多</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                            <span class="token keyword keyword-if">if</span> <span class="token number">0</span> <span class="token operator">&lt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> 开仓挂单数 <span class="token keyword keyword-and">and</span> ccss <span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">+</span> n1
                                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂平仓"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 平仓已成交"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>  <span class="token comment"># 平仓挂单列表为空</span>
                <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">True</span>
                    <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                        zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>

        <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&lt;=</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>
            zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-if">if</span> shijian <span class="token operator">==</span> <span class="token number">145800</span> <span class="token keyword keyword-or">or</span> shijian <span class="token operator">==</span> <span class="token number">225800</span><span class="token punctuation">:</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
            <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
                <span class="token comment"># 清仓</span>
                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia<span class="token operator">-</span>n1
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ccss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平多</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 收盘工作,记录收盘价撤单等"</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8'</span>
                shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span> <span class="token operator">+</span> cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token keyword keyword-return">return</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> zuixinjia<span class="token punctuation">,</span>  cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>


<span class="token keyword keyword-def">def</span> 根据底仓单挂网格空_rn<span class="token punctuation">(</span>cl<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    启动方式，为轮询
    主观判断建好仓位后建好底仓后，价格反方向运动n1跳加仓1手，盈利n跳平一手，反复操作，加仓和止盈都为挂单,只多,单挂，实盘持仓不会越来越大
    1、过夜跳空开空多少就补多少
    2、底仓被止盈完后，就按底仓方向开一手，一直这样循环
    3、每次都加仓1手
    收盘清仓
    """</span>
    <span class="token comment"># 数据提取区</span>
    Open <span class="token operator">=</span> cl<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">.</span>values  <span class="token comment"># 开盘价np列表</span>
    Datetime <span class="token operator">=</span> cl<span class="token punctuation">.</span>klines<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>values  <span class="token comment"># 日期时间np列表，为时间戳</span>
    riqishijian <span class="token operator">=</span> time_to_datetime<span class="token punctuation">(</span>Datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    shijian <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>riqishijian<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%H%M%S"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 当前时间,格式为20210828</span>
    chicang <span class="token operator">=</span> cl<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangzhuangtai'</span><span class="token punctuation">]</span>
    chicangshuliang <span class="token operator">=</span> cl<span class="token punctuation">.</span>openingdata<span class="token punctuation">[</span><span class="token string">'kaicangshuliang'</span><span class="token punctuation">]</span> <span class="token operator">*</span> chicang

    <span class="token comment"># 参数设置区</span>
    ss <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 开仓数量</span>
    n1 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 加仓止盈价格间隔，为跳</span>
    夜盘收盘时间 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
    清零数据库 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span>  <span class="token comment"># 为0无用，为1时初始化数据库</span>
    开仓挂单数 <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span>
    zhanghu <span class="token operator">=</span> cl<span class="token punctuation">.</span>zhanghu<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
    heyue <span class="token operator">=</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 交易的合约</span>
    不交易时间 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">210000</span><span class="token punctuation">,</span> <span class="token number">210100</span><span class="token punctuation">,</span> <span class="token number">210200</span><span class="token punctuation">,</span> <span class="token number">210300</span><span class="token punctuation">,</span> <span class="token number">210400</span><span class="token punctuation">,</span> <span class="token number">90000</span><span class="token punctuation">,</span> <span class="token number">90100</span><span class="token punctuation">,</span> <span class="token number">90200</span><span class="token punctuation">,</span> <span class="token number">90300.90400</span><span class="token punctuation">,</span> <span class="token number">145700</span><span class="token punctuation">,</span> <span class="token number">145800</span><span class="token punctuation">,</span> <span class="token number">145900</span><span class="token punctuation">,</span> <span class="token number">225700</span><span class="token punctuation">,</span>
             <span class="token number">225800</span><span class="token punctuation">,</span> <span class="token number">225900</span><span class="token punctuation">]</span>

    <span class="token comment"># 指标计算区</span>
    <span class="token comment"># 初始化，初始化策略，和更新时运行一次</span>
    <span class="token keyword keyword-if">if</span> <span class="token string">'zxbd'</span> <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">:</span>  <span class="token comment"># 初始化时执行一次，用于初始化缓存字典</span>
        <span class="token keyword keyword-if">if</span> <span class="token keyword keyword-not">not</span> shujuku<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token keyword keyword-or">or</span> 清零数据库 <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 数据库中没有此字段，初始化数据库</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>get_zuixiaobiandong<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 交易合约最小变动单位</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8'</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 0，为初始状态，1为收盘，2为开盘</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span>
            shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>

        durushuju <span class="token operator">=</span> shujuku<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
        cl<span class="token punctuation">.</span>sjb <span class="token operator">=</span> ast<span class="token punctuation">.</span>literal_eval<span class="token punctuation">(</span>durushuju<span class="token punctuation">)</span>

    n1 <span class="token operator">=</span> n1 <span class="token operator">*</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'zxbd'</span><span class="token punctuation">]</span>
    ccss <span class="token operator">=</span> <span class="token punctuation">(</span>chazhaoheyue_duiyingchicang<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>df_cc<span class="token punctuation">,</span> cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    <span class="token comment"># 账户持仓放入显示</span>
    <span class="token keyword keyword-if">if</span> ccss <span class="token operator">!=</span> chicangshuliang<span class="token punctuation">:</span>
        MubiaoCangwei4_bxd<span class="token punctuation">(</span>ccss<span class="token punctuation">,</span> Open<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl<span class="token punctuation">)</span>
    ccss <span class="token operator">=</span> <span class="token builtin">abs</span><span class="token punctuation">(</span>ccss<span class="token punctuation">)</span>
    <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>shangcishijian <span class="token operator">!=</span> Datetime<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span>  <span class="token comment"># 收盘运行</span>
        shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span><span class="token operator">+</span>cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 持久化全局变量</span>

    <span class="token comment"># zuixinjia = cl.tick['LastPrice']  # 最新价</span>
    zuixinjia <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token punctuation">(</span>DuquRedis_HQ<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'LastPrice'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>jiaoyiqidong<span class="token punctuation">:</span>  <span class="token comment"># 更新k线时执行，一般用与计算指标</span>
        <span class="token comment"># 初次挂单,或补单挂单</span>
        <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword keyword-or">or</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                lsbl <span class="token operator">=</span> 开仓挂单数 <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;=</span> 开仓挂单数 <span class="token keyword keyword-else">else</span> ccss
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>lsbl<span class="token punctuation">)</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">-</span> n1
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平空</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>开仓挂单数<span class="token punctuation">)</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token operator">+</span>n1
                    cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开空</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>

                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>  <span class="token comment"># 初始化开后盘状态</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> <span class="token string">"初始挂单"</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>  <span class="token comment"># 底仓没有了进行补仓</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia<span class="token operator">-</span>n1
                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'基准价'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>  <span class="token comment"># 开空</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>
                zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂"</span><span class="token punctuation">)</span>

        <span class="token comment"># 有成交后挂单</span>
        <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行开仓挂单</span>
                    <span class="token keyword keyword-if">if</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 开仓已成交，补挂平仓</span>
                        <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> 开仓挂单数<span class="token punctuation">:</span>
                            zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 撤上次止盈单</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

                        <span class="token keyword keyword-if">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kpjg'</span><span class="token punctuation">]</span> <span class="token operator">-</span> n1
                            cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>  ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平空</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                            <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> 开仓挂单数<span class="token punctuation">:</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">+</span> n1
                                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开空</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂开仓"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 开仓挂单已成交"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>  <span class="token comment"># 开仓挂单列表为空</span>
                    <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                        cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">True</span>
                        <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                            zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>

            <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword keyword-in">in</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">:</span>  <span class="token comment"># 已进行平仓挂单</span>
                    <span class="token keyword keyword-if">if</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kcdsl'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>  <span class="token comment"># 平仓挂单已成交</span>
                        <span class="token keyword keyword-if">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> 开仓挂单数<span class="token punctuation">:</span>
                            zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 开仓挂单太多进行测单</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

                        <span class="token keyword keyword-if">if</span> <span class="token boolean">True</span><span class="token punctuation">:</span>  <span class="token comment"># 底仓为1手，已成交的话，这里的ccss等于1，但实际已经为空仓了</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zhanghu<span class="token punctuation">.</span>order_dict<span class="token punctuation">[</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'kpjg'</span><span class="token punctuation">]</span> <span class="token operator">+</span> n1
                            cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            <span class="token keyword keyword-del">del</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>kc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>  ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 开空</span>
                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                            <span class="token keyword keyword-if">if</span> <span class="token number">0</span> <span class="token operator">&lt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> 开仓挂单数 <span class="token keyword keyword-and">and</span> ccss <span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> n1
                                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>  <span class="token comment"># 下单价格</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平空</span>
                                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 补挂平仓"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

                            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 平仓已成交"</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>  <span class="token comment"># 平仓挂单列表为空</span>
                <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                    cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单为空'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">True</span>
                    <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                        zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>

        <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&lt;=</span> <span class="token number">0</span> <span class="token keyword keyword-and">and</span> shijian <span class="token keyword keyword-not">not</span> <span class="token keyword keyword-in">in</span> 不交易时间<span class="token punctuation">:</span>
            zhanghu<span class="token punctuation">.</span>query_positions<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 查询持仓</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-if">if</span> shijian <span class="token operator">==</span> <span class="token number">145800</span> <span class="token keyword keyword-or">or</span> shijian <span class="token operator">==</span> <span class="token number">225800</span><span class="token punctuation">:</span>
            cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘价'</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia
            <span class="token keyword keyword-if">if</span> ccss <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                <span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                    zhanghu<span class="token punctuation">.</span>cd_orders<span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 撤上次开仓挂单</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
                <span class="token comment"># 清仓</span>
                cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> zuixinjia<span class="token operator">+</span>n1
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>zhanghu<span class="token punctuation">.</span>pc_orders<span class="token punctuation">(</span>cl<span class="token punctuation">.</span>jiaoyishezhi<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ccss<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 平空</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'收盘状态'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>
                time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>heyue<span class="token punctuation">,</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">"%Y-%m-%d %H:%M:%S"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">" 收盘工作,记录收盘价撤单等"</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'开仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'平仓jg'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
                cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'补单id'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'8'</span>
                shujuku<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'celue.'</span> <span class="token operator">+</span> cl<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">(</span>cl<span class="token punctuation">.</span>sjb<span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token keyword keyword-return">return</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂单状态'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> zuixinjia<span class="token punctuation">,</span>  cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂开仓价格'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cl<span class="token punctuation">.</span>sjb<span class="token punctuation">[</span><span class="token string">'挂平仓价格'</span><span class="token punctuation">]</span>

</code></pre><hr>
<h4 id="33-策略挂单效果展示">3.3、策略挂单效果展示 </h4>
<p><img src="../img/%E7%BD%91%E6%A0%BC%E7%AD%96%E7%95%A5/%E5%8D%95%E6%8C%82%E7%BD%91%E6%A0%BC%E6%8C%82%E5%8D%95.png" alt="在这里插入图片描述"></p>
<hr>
<h4 id="34-刷了30分钟效果还行">3.4、刷了30分钟效果还行 </h4>
<p><img src="../img/%E7%BD%91%E6%A0%BC%E7%AD%96%E7%95%A5/%E5%8D%95%E6%8C%82%E7%BD%91%E6%A0%BC%E7%9B%88%E4%BA%8F.png" alt="在这里插入图片描述"></p>
<hr>
<h4 id="35-高级刷单网格工作区">3.5、高级刷单网格工作区 </h4>
<p><img src="../img/%E7%BD%91%E6%A0%BC%E7%AD%96%E7%95%A5/%E5%8D%95%E6%8C%82%E7%BD%91%E6%A0%BC%E5%B7%A5%E4%BD%9C%E5%8C%BA.png" alt="在这里插入图片描述"></p>
<hr>

      </div>
      
      
    
    
    
    
    
    
  
    </body></html>